package com.example.exam.service.impl;

import com.example.exam.entity.ExamPaper;
import com.example.exam.repository.ExamPaperRepository;
import com.example.exam.service.ExamPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.Optional;


/**
 * @author shishuqian
 * date 2021/4/27
 * time 15:27
 **/
@Service
public class ExamPaperServiceImpl implements ExamPaperService {

    @Autowired
    private ExamPaperRepository examPaperRepository;

    @Override
    public Page<ExamPaper> findAllByUserId(Integer user_id, Integer page, Integer size) {
        //排序
        //Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
        //分页
        Pageable pageable =  PageRequest.of(page-1,size);;
        //分页查询
        Page<ExamPaper> examPapers = examPaperRepository
                .findByCreateUserIdAndDeletedOrderByCreateTimeDesc(user_id,false, pageable);

        for (ExamPaper examPaper:examPapers){
            examPaper.setQuestionList(null);
        }


        return examPapers;
    }

    @Override
    public ExamPaper findById(Integer id) {

        Optional<ExamPaper> examPaper = examPaperRepository.findById(id);
        if (examPaper.isPresent()){
            ExamPaper paper = examPaper.get();
            //对question根据orderNum进行从小到大排序
            Collections.sort(paper.getQuestionList());
            return paper;
        }else {
            return null;
        }

    }

    @Override
    public int save(ExamPaper examPaper) {
        examPaper.setDeleted(false);
        ExamPaper examPaper1 = examPaperRepository.save(examPaper);
        return examPaper1.getId();
    }

    @Override
    public void update(ExamPaper examPaper) {
        examPaperRepository.save(examPaper);
    }

    @Override
    public void deleteById(Integer id) {
        ExamPaper examPaper = examPaperRepository.findById(id).get();
        examPaper.setDeleted(true);
        examPaperRepository.save(examPaper);
    }

    @Override
    public void release(Integer id) {
        ExamPaper examPaper = examPaperRepository.findById(id).get();
        if (examPaper.getStatus()==null){
            examPaper.setStatus(true);
        }else{
            examPaper.setStatus(!examPaper.getStatus());
        }

        examPaperRepository.save(examPaper);
    }

    @Override
    public Page<ExamPaper> findByTitleLike(String title,Integer userId,Integer page,Integer size) {
        //分页
        Pageable pageable =  PageRequest.of(page-1,size);
        Page<ExamPaper> examPapers = examPaperRepository
                .findByTitleLikeAndCreateUserIdOrderByCreateTimeDesc(title,userId,pageable);
        for (ExamPaper examPaper:examPapers){
            examPaper.setQuestionList(null);
        }

        return examPapers;
    }
}
